/* Developed by Vinh, Nguyen Hong <mansofta@gmail.com>
 * Copyright HIENDAI SOFTWARE COMPANY 2004
 * http://www.hiendai.com.vn
 * contact@hiendai.com.vn
*/
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace MarcXmlParser {

	public class CControlfield
	{	
	    protected	XmlDocument ptCurrentDoc;
        protected   XmlNode ptCurrentRootNode;		
	 
	public CControlfield()
		{
			ptCurrentDoc=new XmlDocument();				
			ptCurrentDoc.LoadXml("<controlfield tag='000'></controlfield>"); 
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='controlfield']"); 		
		}
    public void ReConstruct()
		{
			ptCurrentDoc.LoadXml("<controlfield tag='000'></controlfield>"); 
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='controlfield']"); 				
		}
    public bool load_RootNode(ref XmlNode ptControlfieldNode)
		{
			ptCurrentRootNode=ptControlfieldNode;			
			if(ptCurrentRootNode.Name=="controlfield")
				return true;
			else
				return false;// invalid node name
		}
        public bool load_Xml(String ControlfieldXml)
		{
			ptCurrentDoc.LoadXml(ControlfieldXml);
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='controlfield']");
			if (ptCurrentRootNode!=null)
				return true;
			else
				return false;
		}	
		// TODO: Add your methods for this class here.
        public String Tag
	 {
		 get
		 {
			XmlNode mxNode = (XmlNode)(ptCurrentRootNode.Attributes.GetNamedItem("tag"));
			if(mxNode!=null)
				return ((XmlAttribute)(mxNode)).Value;
			else
				return "";			
		 }
		 set
		 {
			XmlElement xElem;//Data field node			
			xElem=(XmlElement)(ptCurrentRootNode); 
			xElem.SetAttribute("tag", value);				 
		 }	 
	 }
        public String Value
	 {
		 set
		 {
			XmlNode ptEditNode;
			if (ptCurrentRootNode.HasChildNodes) //child text node have been existed
			{
				ptEditNode=ptCurrentRootNode.FirstChild;
				ptEditNode.Value=value; 					
			} else // Create new text node then add to the parent node
			{
				ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateTextNode(value));
			}		 
		 }
		 get
		 {
			if(ptCurrentRootNode!=null)
				return ptCurrentRootNode.InnerText;
			else
				return "";
		 }	 
	 }

        public void set_ValueByPos(String sValue, int StartPos, int EndPos)
		{
			XmlNode ptEditNode;
			StringBuilder Sb = new StringBuilder();
			StringBuilder SbValue=new StringBuilder(sValue);
			int Valen=sValue.Length; 
			if (Valen==0)
				return;
			if (StartPos>EndPos)
				return;
			if (Valen>(EndPos+1-StartPos))//Value length greater than space
				{
					SbValue.Remove(EndPos+1-StartPos,Valen-(EndPos+1-StartPos)); 
				}
			if (Valen<(EndPos+1-StartPos))//Value length smaller than space
				{
					SbValue.Insert(Valen," ",(EndPos+1-StartPos)-Valen); 
				}
			if (ptCurrentRootNode.HasChildNodes) //child text node have been existed
			{
				ptEditNode=ptCurrentRootNode.FirstChild;
				Sb.Append(ptEditNode.InnerText); 
				if(Sb.Length<EndPos+1)//not enough space
				{
					Sb.Append(' ',(EndPos+Valen+1)-Sb.Length);  																									
				}				
				Sb.Insert(StartPos,SbValue.ToString());
				Sb.Remove(EndPos+1,SbValue.Length);  
				ptEditNode.Value=Sb.ToString(); 					
			} else // Create new text node then add to the parent node
			{
				Sb.Append(' ',EndPos+Valen+1);  																									
				Sb.Insert(StartPos,SbValue.ToString());
				ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateTextNode(Sb.ToString()));
			}
		}

        public String get_ValueByPos(int StartPos, int EndPos)
		{
			StringBuilder Sb = new StringBuilder();				
			String TempStr;
			if((EndPos+1-StartPos)<=0) 
				return "";
			if(ptCurrentRootNode!=null)
				Sb.Append(ptCurrentRootNode.InnerText);
			else
				return "";
			if (Sb.Length>=StartPos)
			{
				if (Sb.Length<(EndPos+1))
				{
					EndPos=Sb.Length-1;
				}
				TempStr=Sb.ToString();				
				return TempStr.Substring(StartPos,(EndPos+1-StartPos));  
			}else
			{
				return "";
			}
		}
        public String OuterXml
		{
			get
			{
				return ptCurrentRootNode.OuterXml ;  					
			}
		}
        public XmlNode RootNode
		{
			set
			{
				ptCurrentRootNode=value;
				ptCurrentDoc=ptCurrentRootNode.OwnerDocument;
			}			
			get
			{
				return ptCurrentRootNode;
			}			
		}
	};
}